using System;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class MissionBase : MonoBehaviour
{
	public int numCurStep;

	public string nameScene = string.Empty;

	public bool offlineMission = true;

	public string title = string.Empty;

	public string msgPurpose = string.Empty;

	[TextArea]
	public string msgManualStart = string.Empty;

	[TextArea]
	public string msgManualEnd = string.Empty;

	public int CountAndAddExeptions;

	public int GiveExp;

	public int GiveReputation;

	public bool isStarted;

	public bool isCompleted;

	public List<TargetParameter> listTargetParams = new List<TargetParameter>();

	private int lastStepMission;

	public bool NeedForceStop;

	[SerializeField]
	private string startDistrictName = string.Empty;

	private void Awake()
	{
		foreach (TargetParameter listTargetParam in listTargetParams)
		{
			listTargetParam.curMission = this;
			listTargetParam.UpdateIsCompletedTarget();
		}
	}

	public void OnStartMission()
	{
		startDistrictName = DistrictDetector.instance.currentDistrictName;
		if (listTargetParams.Count > 0)
		{
			if (BCWDebug.isDebugBuild)
			{
				BCWDebug.Log("OnStartMission = " + base.gameObject.name);
			}
			if (!isCompleted)
			{
				isStarted = true;
			}
			else
			{
				OnCompleteMission();
			}
			numCurStep = 0;
			lastStepMission = listTargetParams.Count;
			StartTargetCurrentStep();
		}
		else
		{
			OnCompleteMission();
		}
	}

	public void OnStopMission()
	{
		for (int i = 0; i < listTargetParams.Count; i++)
		{
			listTargetParams[i].OnStopTarget();
		}
	}

	public void OnUpdateMission()
	{
		if (isStarted)
		{
			if (listTargetParams[numCurStep].isStarted)
			{
				listTargetParams[numCurStep].target.OnUpdateTarget();
			}
			if (listTargetParams[numCurStep].isComplete)
			{
				NextStepMission();
			}
		}
	}

	public void OnCompleteMission()
	{
		if (!isCompleted)
		{
			isCompleted = true;
			ManagerMissions.instance.ShowMissionCompleate();
			settings.CathedExeptions += CountAndAddExeptions;
			SessionReputationManager.Instance.AddReputation(GiveReputation);
			ManagerMissions.OnCompleteMission();
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("Quest_Finish", title);
			FlurryWrapper.LogEvent(FlurryWrapper.EV_QUESTS_FINISHED, dictionary);
		}
	}

	public bool MissionWithCheckpoint()
	{
		return listTargetParams[0].target.gameObject.GetComponent<MissionTargetCheckpoint>() != null;
	}

	public bool MissionIsComplete()
	{
		foreach (TargetParameter listTargetParam in listTargetParams)
		{
			if (listTargetParam.curMission == null)
			{
				listTargetParam.curMission = this;
			}
			listTargetParam.UpdateIsCompletedTarget();
			if (!listTargetParam.isComplete)
			{
				return false;
			}
			listTargetParam.target.OnStopTarget();
		}
		return true;
	}

	private void NextStepMission()
	{
		if (!isCompleted)
		{
			numCurStep++;
			if (numCurStep == lastStepMission)
			{
				OnCompleteMission();
			}
			else
			{
				StartTargetCurrentStep();
			}
		}
	}

	private void StartTargetCurrentStep()
	{
		if (!listTargetParams[numCurStep].isStarted && isStarted && GameController.instance != null)
		{
			listTargetParams[numCurStep].StartTarget();
		}
	}

	public TargetParameter StartedTarget()
	{
		if (numCurStep >= listTargetParams.Count)
		{
			return null;
		}
		return listTargetParams[numCurStep];
	}

	public void ResetMission()
	{
		if (BCWDebug.isDebugBuild)
		{
			BCWDebug.Log("ResetMission: " + base.gameObject.name);
		}
		foreach (TargetParameter listTargetParam in listTargetParams)
		{
			ResetTarget(listTargetParam);
			listTargetParam.SetIsCompletedTarget(false);
			listTargetParam.OnStopTarget();
		}
		OnStartMission();
	}

	private void ResetTarget(TargetParameter tp)
	{
		if (tp.curMission != null)
		{
			string text = base.gameObject.name + "_" + tp.target.gameObject.name;
			BCWStorage.Set(text, 0);
			string key = text + TargetParameter.keyCompleted;
			BCWStorage.Set(key, false);
		}
	}
}
